package com.sfilyh.servelt.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sfilyh.util.DBUtil;

@WebServlet("/bz-admin/delCategory")
public class delCategory extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 String id =  request.getParameter("id");
	 boolean deleted = false;
	 String errorMessage = "参数错误，请重试!";
	 
	 if(id != null){
		int count =  DBUtil.countResutSize("select id from tb_article where category = ? and status <> 2 ", Integer.parseInt(id));
		errorMessage = "删除失败，分类下有文章！";
		if(count == 0){
			DBUtil.update("delete from tb_category  where id = ? ", Integer.parseInt(id));
			errorMessage = "删除成功！";
			deleted = true;
		}
	 }	 
	 
	 request.setAttribute("msg", errorMessage);
	 request.setAttribute("toUrl", request.getContextPath()+"/bz-admin/listCategory");
	 request.getRequestDispatcher("/WEB-INF/views/toUrl.jsp").forward(request, response);
	}
}
